package com.changil.admin.protocol.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.HttpClientBuilder;

public class HttpUtil {
	private static final String BASE_URL = "http://changil153.vps.phps.kr/NowonChangilChurch_Server/api";

	public static void forward(HttpServletRequest request,
			HttpServletResponse response, String path) {
		try {

			RequestDispatcher dispatcher = request.getRequestDispatcher(path);
			dispatcher.forward(request, response);

		} catch (Exception ex) {
			throw new RuntimeException("forward Exception : " + ex);
		}
	}

	public static void redirect(HttpServletResponse response, String path) {
		try {
			System.out.println("패스는 " + path);
			response.sendRedirect(path);
		} catch (Exception ex) {
			throw new RuntimeException("redirect Exception  : " + ex);
		}
	}

	// HTTP GET request
	public static String get(String url) {
		HttpURLConnection conn = null;
		BufferedReader in = null;
		StringBuffer response = new StringBuffer();
		try {
			URL obj = new URL(BASE_URL + url);
			conn = (HttpURLConnection) obj.openConnection();

			// optional default is GET
			conn.setRequestMethod("GET");

			int responseCode = conn.getResponseCode();
			System.out.println("\nSending 'GET' request to URL : " + url);
			System.out.println("Response Code : " + responseCode);

			in = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "utf-8"));
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			System.out.println(response.toString());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != conn) {
				conn.disconnect();
			}
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return response.toString();
	}

	// HTTP POST request
	public static String post(String url, List<NameValuePair> nameValuePairs) {
		HttpClient httpclient = HttpClientBuilder.create().build();;
		HttpPost httppost = new HttpPost(BASE_URL + url);
		BufferedReader in = null;
		StringBuffer result = new StringBuffer();
		// Add your data
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Execute HTTP Post Request
		try {
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent(), "utf-8"));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				result.append(inputLine);
			}
			return result.toString();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
//		HttpURLConnection conn = null;
//		BufferedReader in = null;
//		StringBuffer response = new StringBuffer();
//		try {
//			URL obj = new URL(BASE_URL + url);
//			conn = (HttpURLConnection) obj.openConnection();
//			// add reuqest header
//			conn.setRequestMethod("POST");
//			// Send post request
//			conn.setDoOutput(true);
//			DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
//			wr.writeBytes(params);
//			wr.flush();
//			wr.close();
//			
//			
//			int responseCode = conn.getResponseCode();
//			System.out.println("\nSending 'POST' request to URL : " + url);
//			System.out.println("Response Code : " + responseCode);
//			in = new BufferedReader(new InputStreamReader(
//					conn.getInputStream(), "utf-8"));
//			String inputLine;
//			while ((inputLine = in.readLine()) != null) {
//				response.append(inputLine);
//			}
//			System.out.println(response.toString());
//		} catch (MalformedURLException e) {
//			e.printStackTrace();
//		} catch (ProtocolException e1) {
//			e1.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (null != conn) {
//				conn.disconnect();
//			}
//			if (null != in) {
//				try {
//					in.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
//		return response.toString();
	}
	
	// HTTP DELETE request
		public static String put(String url, List<NameValuePair> nameValuePairs) {
			HttpClient httpclient = HttpClientBuilder.create().build();;
			HttpPut httpput = new HttpPut(BASE_URL + url);
			BufferedReader in = null;
			StringBuffer result = new StringBuffer();
			
			try {
				httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// Add your data
			try {
				HttpResponse response = httpclient.execute(httpput);
				in = new BufferedReader(new InputStreamReader(
						response.getEntity().getContent(), "utf-8"));
				String inputLine;
				while ((inputLine = in.readLine()) != null) {
					result.append(inputLine);
				}
				return result.toString();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
//			HttpURLConnection conn = null;
//			BufferedReader in = null;
//			StringBuffer response = new StringBuffer();
//			try {
//				URL obj = new URL(BASE_URL + url);
//				conn = (HttpURLConnection) obj.openConnection();
//				// add reuqest header
//				conn.setRequestMethod("PUT");
//				// Send post request
//				conn.setDoOutput(true);
//				DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
//				wr.writeBytes(params);
//				wr.flush();
//				wr.close();
//				
//				
//				int responseCode = conn.getResponseCode();
//				System.out.println("\nSending 'PUT' request to URL : " + url);
//				System.out.println("Response Code : " + responseCode);
//				in = new BufferedReader(new InputStreamReader(
//						conn.getInputStream(), "utf-8"));
//				String inputLine;
//				while ((inputLine = in.readLine()) != null) {
//					response.append(inputLine);
//				}
//				System.out.println(response.toString());
//			} catch (MalformedURLException e) {
//				e.printStackTrace();
//			} catch (ProtocolException e1) {
//				e1.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			} finally {
//				if (null != conn) {
//					conn.disconnect();
//				}
//				if (null != in) {
//					try {
//						in.close();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
//			return response.toString();
		}

	// HTTP DELETE request
	public static String delete(String url) {
		HttpURLConnection conn = null;
		BufferedReader in = null;
		StringBuffer response = new StringBuffer();
		try {
			URL obj = new URL(BASE_URL + url);
			conn = (HttpURLConnection) obj.openConnection();
			// add reuqest header
			conn.setRequestMethod("DELETE");
			// Send post request
			conn.setDoOutput(true);
			int responseCode = conn.getResponseCode();
			System.out.println("\nSending 'DELETE' request to URL : " + url);
			System.out.println("Response Code : " + responseCode);
			in = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "utf-8"));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			System.out.println(response.toString());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != conn) {
				conn.disconnect();
			}
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return response.toString();
	}

}